package com.card.manager.services.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.card.framework.mybatis.mapper.BatchCallBack;
import com.card.framework.mybatis.mapper.MyFrameworkSupperMapper;
import com.card.framework.mybatis.optimist.ModifiedByAnotherUserException;
import com.card.framework.page.model.PageModel;
import com.card.manager.dao.MadMapper;
import com.card.manager.domain.Mad;
import com.card.manager.domain.MadExample;
import com.card.manager.domain.MadExample.Criteria;
import com.card.manager.services.MadService;
@Service("MadServiceImpl")
public class MadServiceImpl extends MyFrameworkSupperMapper implements MadService {

	@Override
	public Map<String, Object> selectMadForPage(Mad mad, PageModel pageModel) {
		MadMapper mapper = this.getMapper(MadMapper.class);
		MadExample madExample = new MadExample();
		Criteria oredCriteria  = madExample.createCriteria();
		madExample.setPage(pageModel);
		if(StringUtils.isNotEmpty(pageModel.getSort())) 
		madExample.setOrderByClause(pageModel.getSort()+" "+pageModel.getOrder());
//		if(StringUtils.isNotEmpty(mad.getUsername())) 
//			oredCriteria.andUsernameEqualTo(mad.getUsername());
//		if(StringUtils.isNotEmpty(mad.getOwnercard())) 
//			oredCriteria.andOwnercardEqualTo(mad.getOwnercard());
//		if(StringUtils.isNotEmpty(mad.getTelephone())) 
//			oredCriteria.andTelephoneEqualTo(mad.getTelephone());
		//
		if(mad.getStartdate() != null){
			oredCriteria.andStartdateLessThanOrEqualTo(mad.getStartdate());
		}
		if(mad.getEnddate() != null){
			oredCriteria.andEnddateGreaterThanOrEqualTo(mad.getEnddate());
		}
		if(StringUtils.isNotBlank(mad.getTitle()))
			oredCriteria.andTitleLike("%" + mad.getTitle() + "%");
		if(StringUtils.isNotBlank(mad.getValue()))
			oredCriteria.andValueLike("%" + mad.getValue() + "%");
		
		oredCriteria.andDelflgEqualTo((byte)1);
		
		List<Mad> list = mapper.selectByExample(madExample);
		Map<String, Object> modelMap = new HashMap<String, Object>();  
	    modelMap.put("total", pageModel.getTotalCount());  
	    modelMap.put("rows", list); 
	    return modelMap;
	}

	@Override
	public void deleteMads(final List<Mad> mad)
			throws ModifiedByAnotherUserException, Exception {
		batchSubmit(MadMapper.class,new BatchCallBack() {
			@Override
			public <E> void batchCallBack(E mapper) {
				if(mad != null && mad.size() > 0) {
					for(Mad a :mad){
						a.setDelflg((byte)0);
						((com.card.manager.dao.MadMapper) mapper).updateByPrimaryKeySelective(a);
					}
				}
			}
		});
	}

	@Override
	public void addOrUpdate(Mad mad) {
		MadMapper mapper = this.getMapper(MadMapper.class);
		if(mad.getVersion()==null){
			mad.setVersion((long)1);
			mad.setDelflg((byte)1);
			mapper.insert(mad);
		}else
		{
			MadExample madExample = new MadExample();
			madExample.createCriteria().andIdEqualTo(mad.getId());
			mapper.updateByExampleSelective(mad, madExample);
		}
	}

	@Override
	public Mad getById(long id) {
		MadMapper mapper = this.getMapper(MadMapper.class);
	    return mapper.selectByPrimaryKey(id);
	}
	
	public int count(Mad mad){
		MadMapper mapper = this.getMapper(MadMapper.class);
		MadExample madExample = new MadExample();
		Criteria oredCriteria  = madExample.createCriteria();
		if(mad.getStartdate() != null){
			oredCriteria.andStartdateLessThanOrEqualTo(mad.getStartdate());
		}
		if(mad.getEnddate() != null){
			oredCriteria.andEnddateGreaterThanOrEqualTo(mad.getEnddate());
		}
		if(StringUtils.isNotBlank(mad.getTitle()))
			oredCriteria.andTitleLike("%" + mad.getTitle() + "%");
		if(StringUtils.isNotBlank(mad.getValue()))
			oredCriteria.andValueLike("%" + mad.getValue() + "%");
		
		oredCriteria.andDelflgEqualTo((byte)1);
		return mapper.countByExample(madExample);
	}
}
